/**
 * @author [siwi]
 * @email [siwi@siwi.me]
 * @create date 2017-11-05 08:22:02
 * @modify date 2017-11-05 08:22:02
 * @desc [description]
*/
const crypto = require('crypto')
const constant = require('../../config/constant')
class Util {
    constructor() {

    }
    async md5 (data) {
        return crypto.createHash('md5', MD5_SECRET).update(data).digest('hex')
    }
    async sha256 (data) {
        return crypto.createHmac('sha256', MD5_SECRET).update(data).digest('hex')
    }

    /**
     * 加密
     */
    async cipher(data) {
        const CIPHER_SECRET = constant.CIPHER_SECRET()
        const cipher = crypto.createCipher('aes192', CIPHER_SECRET)
        let encrypted = cipher.update(data, 'utf8', 'hex')
        encrypted += cipher.final('hex')
        return  encrypted
    }
    /**
     * 解密
     */
    async decipher (data) {
        const DECIPHER_SECRET = constant.DECIPHER_SECRET()
        const decipher = crypto.createDecipher('aes192', DECIPHER_SECRET)
        let decrypted = decipher.update(data, 'hex', 'utf8')
        decrypted += decipher.final('utf8')
        return  decrypted
    }
}

module.exports = new Util()